/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DetalleGrupo.java
 *
 * Created on 09-sep-2011, 9:21:41
 */
package Ventanas;

import Objetos.PropiedadesIP;
import Proxies.CtrlDetalleDeGrupoWS;
import Proxies.CtrlDetalleDeGrupoWSService;
import Proxies.DataEventoArray;
import Proxies.DataGrupoArray;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.xml.namespace.QName;

/**
 *
 * @author Sebas
 */
public class DetalleGrupo extends javax.swing.JFrame {

    CtrlDetalleDeGrupoWS ctrlDetalles;
    
    /** Creates new form DetalleGrupo */
    public DetalleGrupo() {
        
        initComponents();
        
        PropiedadesIP props = PropiedadesIP.getInstance();
        String IP = props.getServidorCentralIP();
        
        URL dirGr = null;
        try {
            dirGr = new URL(IP + "CtrlDetalleGrupo?wsdl");
        } catch (MalformedURLException ex) {
            Logger.getLogger(DetalleGrupo.class.getName()).log(Level.SEVERE, null, ex);
        }
        QName qNaGr = new QName("http://WebServices/", "CtrlDetalleDeGrupoWSService");
        ctrlDetalles = new CtrlDetalleDeGrupoWSService(dirGr, qNaGr).getCtrlDetalleDeGrupoWSPort();

        actualizarGrupos();
        actualizarCalendarioEventos ();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jLabel17 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jComboBox_Grupos = new javax.swing.JComboBox();
        jScrollPane4 = new javax.swing.JScrollPane();
        JTable_Miembros = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel_NomGrupo = new javax.swing.JLabel();
        jLabel_DescGrupo = new javax.swing.JLabel();
        jLabel_FechaCreacionGrupo = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        listEventos = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        vNombre = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        vTipo = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        vRep = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        vFecha = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        listFechas = new javax.swing.JList();
        jScrollPane3 = new javax.swing.JScrollPane();
        jListMuro = new javax.swing.JList();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        jLabel17.setText("jLabel17");

        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel6.setText("Seleccionar grupo del sistema:");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, -1));

        jLabel1.setText("RED - Ver Detalles de Grupo");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(207, 11, -1, -1));

        jComboBox_Grupos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox_GruposActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox_Grupos, new org.netbeans.lib.awtextra.AbsoluteConstraints(200, 40, -1, -1));

        JTable_Miembros.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nick", "Email", "Nombre", "Rol del usuario"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane4.setViewportView(JTable_Miembros);
        JTable_Miembros.getColumnModel().getColumn(0).setResizable(false);
        JTable_Miembros.getColumnModel().getColumn(1).setResizable(false);
        JTable_Miembros.getColumnModel().getColumn(2).setResizable(false);
        JTable_Miembros.getColumnModel().getColumn(3).setResizable(false);

        getContentPane().add(jScrollPane4, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 190, 512, 131));

        jLabel2.setText("Miembros del grupo:");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 190, -1, -1));

        jLabel3.setText("Nombre:");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(65, 89, -1, -1));

        jLabel4.setText("Fecha de creación:");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 160, -1, -1));

        jLabel5.setText("Descripción:");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(48, 109, -1, -1));

        jLabel7.setText("Imagen:");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(66, 129, -1, -1));

        jLabel_NomGrupo.setText("NombreGrupo");
        getContentPane().add(jLabel_NomGrupo, new org.netbeans.lib.awtextra.AbsoluteConstraints(124, 89, -1, -1));

        jLabel_DescGrupo.setText("DescGrupo");
        getContentPane().add(jLabel_DescGrupo, new org.netbeans.lib.awtextra.AbsoluteConstraints(124, 109, -1, -1));

        jLabel_FechaCreacionGrupo.setText("FechaCreacionGrupo");
        getContentPane().add(jLabel_FechaCreacionGrupo, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 160, -1, -1));

        jLabel8.setText("DETALLES DE GRUPO:");
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 68, -1, -1));

        jLabel9.setText("Calendario de Eventos :");
        getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 363, -1, -1));

        listEventos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listEventosActionPerformed(evt);
            }
        });
        getContentPane().add(listEventos, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 360, 186, -1));

        jLabel10.setText("DETALLES DEl EVENTO:");
        getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(412, 363, -1, -1));

        jLabel11.setText("Nombre:");
        getContentPane().add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(483, 394, -1, -1));
        getContentPane().add(vNombre, new org.netbeans.lib.awtextra.AbsoluteConstraints(542, 391, 107, -1));

        jLabel12.setText("Tipo:");
        getContentPane().add(jLabel12, new org.netbeans.lib.awtextra.AbsoluteConstraints(500, 432, -1, -1));

        vTipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vTipoActionPerformed(evt);
            }
        });
        getContentPane().add(vTipo, new org.netbeans.lib.awtextra.AbsoluteConstraints(542, 429, 107, -1));

        jLabel13.setText("Repetible / No Repetible");
        getContentPane().add(jLabel13, new org.netbeans.lib.awtextra.AbsoluteConstraints(408, 470, -1, -1));
        getContentPane().add(vRep, new org.netbeans.lib.awtextra.AbsoluteConstraints(542, 467, 107, -1));

        jLabel14.setText("Fecha");
        getContentPane().add(jLabel14, new org.netbeans.lib.awtextra.AbsoluteConstraints(495, 508, -1, -1));
        getContentPane().add(vFecha, new org.netbeans.lib.awtextra.AbsoluteConstraints(542, 505, 107, -1));

        jScrollPane2.setViewportView(listFechas);

        getContentPane().add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(540, 540, 109, 70));

        jScrollPane3.setViewportView(jListMuro);

        getContentPane().add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 453, 330, 190));

        jLabel15.setFont(new java.awt.Font("Times New Roman", 0, 14));
        jLabel15.setText("Entradas de muro");
        getContentPane().add(jLabel15, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 430, -1, -1));
        getContentPane().add(jLabel16, new org.netbeans.lib.awtextra.AbsoluteConstraints(690, 210, -1, -1));
        getContentPane().add(jLabel18, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 650, -1, -1));

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 10));
        jButton1.setText("ver");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 130, 50, 20));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox_GruposActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox_GruposActionPerformed
        // TODO add your handling code here:
        vNombre.setText(null);
        vRep.setText(null);
        vTipo.setText(null);
        vFecha.setText(null);
        actualizarDetallesGrupo();
         actualizarCalendarioEventos ();
         llenarMuro();
         
    }//GEN-LAST:event_jComboBox_GruposActionPerformed

private void vTipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vTipoActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_vTipoActionPerformed

private void listEventosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_listEventosActionPerformed
// TODO add your handling code here:
    actualizarDetalleDeEvento();
}//GEN-LAST:event_listEventosActionPerformed

private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
// TODO add your handling code here:
        Proxies.DataGrupo dg = ctrlDetalles.detallarGrupo(jComboBox_Grupos.getSelectedItem().toString());
 String imagen = dg.getImg();
 ImageIcon i = new ImageIcon();
 i = cargarImagen(imagen);
 JOptionPane.showMessageDialog(null, i);
}//GEN-LAST:event_jButton1MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new DetalleGrupo().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable JTable_Miembros;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox_Grupos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabel_DescGrupo;
    private javax.swing.JLabel jLabel_FechaCreacionGrupo;
    private javax.swing.JLabel jLabel_NomGrupo;
    private javax.swing.JList jList1;
    private javax.swing.JList jListMuro;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JComboBox listEventos;
    private javax.swing.JList listFechas;
    private javax.swing.JTextField vFecha;
    private javax.swing.JTextField vNombre;
    private javax.swing.JTextField vRep;
    private javax.swing.JTextField vTipo;
    // End of variables declaration//GEN-END:variables

    private void actualizarGrupos(){
        
        DataGrupoArray grupos = ctrlDetalles.listarGrupos();
        if (grupos.getItem().isEmpty())
            jComboBox_Grupos.addItem("No hay grupos en el sistema");
        else{
            
            for (int i = 0;i<grupos.getItem().size();i++){
                jComboBox_Grupos.addItem(grupos.getItem().get(i).getNombre());
            }
            
        }
        
    }
    
    private void actualizarDetallesGrupo(){
        
        if (!ctrlDetalles.listarGrupos().getItem().isEmpty()){
            
            actualizarParticipantes();
            Proxies.DataGrupo dg = ctrlDetalles.detallarGrupo(jComboBox_Grupos.getSelectedItem().toString());
            jLabel_NomGrupo.setText(dg.getNombre());
            jLabel_DescGrupo.setText(dg.getDesc());
            
            int dia = dg.getFechaCreacion().toGregorianCalendar().get(Calendar.DATE);
            int mes = dg.getFechaCreacion().toGregorianCalendar().get(Calendar.MONTH);
            int anio = dg.getFechaCreacion().toGregorianCalendar().get(Calendar.YEAR);
            jLabel_FechaCreacionGrupo.setText(anio+"/"+mes+"/"+dia);
            
            //jLabel_ImgGrupo.setText(dg.getImg());
        }
        
    }
    
    private void actualizarTablaMiembros() {
        TableModel modelo = JTable_Miembros.getModel();
        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(modelo);
        JTable_Miembros.setRowSorter(sorter);
        String text = "";
        if (text.length() == 0) {
            sorter.setRowFilter(null);
        } else {
            sorter.setRowFilter(RowFilter.regexFilter(text));
        }
    }

    private void actualizarParticipantes() {
        DefaultTableModel model = new DefaultTableModel();
        JTable_Miembros.setModel(model);
        model.addColumn("Nick");
        model.addColumn("Email");
        model.addColumn("Nombre");
        model.addColumn("Rol del usuario");
        JTable_Miembros.setRowSelectionAllowed(true);
        JTable_Miembros.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        Iterator<Proxies.DataRol> itera = ctrlDetalles.listarRolUsuariosGrupo(
                jComboBox_Grupos.getSelectedItem().toString()).getItem().iterator();
        Proxies.DataRol usr;
        Object[] fila = new Object[4];

        while (itera.hasNext()) {
            usr = itera.next();
            fila[0] = usr.getUsr().getNick();
            fila[1] = usr.getUsr().getCorreo();
            fila[2] = usr.getUsr().getNombre();
            fila[3] = usr.getRolUsr().toString();
            model.addRow(fila);
        }
    }
    private void actualizarCalendarioEventos (){
        
        listEventos.removeAllItems();
        DataEventoArray eventos = ctrlDetalles.listarCalendarioEventos(jComboBox_Grupos.getSelectedItem().toString());
        if (!eventos.getItem().isEmpty())
        {
            Iterator<Proxies.DataEvento> it = eventos.getItem().iterator();
           while (it.hasNext())
           {
               listEventos.addItem(it.next().getNombre());
           }
                       
        }
        
    }
    private void actualizarDetalleDeEvento(){
    
        
        DataEventoArray ev = ctrlDetalles.listarCalendarioEventos(jComboBox_Grupos.getSelectedItem().toString());
        if(ev.getItem().isEmpty())
        {
            vNombre.setText(null);
             vFecha.setText(null);
             vRep.setText(null);
             vTipo.setText(null);
            return;
        }
        if(listEventos.getSelectedIndex()==-1)
            return;
        vNombre.setText(null);
        vFecha.setText(null);
        vRep.setText(null);
        vTipo.setText(null);
        listFechas.removeAll();
        String clave = listEventos.getSelectedItem().toString();
//        DataEvento de = ev.getItem().get(listEventos.getSelectedItem().toString());
        Proxies.DataEvento de = null ;
        Iterator<Proxies.DataEvento> it = ev.getItem().iterator();
        boolean sigue = true;
        while(it.hasNext() && sigue){
            
            de = it.next();
            if(de.getNombre().equals(listEventos.getSelectedItem().toString()))
                sigue = false;
            
        }
        
        
        List<String> dias = de.getDiasDeSemana();
        Calendar cal = Calendar.getInstance();
        int i = dias.size();

        vNombre.setText(de.getNombre());
        if (de.isRepetible()) {
            listFechas.setVisible(true);
            DefaultListModel model = new DefaultListModel();
            vRep.setText("Repetible");
            while (i != 0) {
                model.addElement(de.getDiasDeSemana().get(i - 1));
                listFechas.setModel(model);
                i--;
            }
        } else {
            vRep.setText("No Repetible");
            cal = de.getFecha().toGregorianCalendar();
            int dia = cal.get(Calendar.DATE);
            int mes = cal.get(Calendar.MONTH);
            int anio = cal.get(Calendar.YEAR);
            int hora = cal.get(Calendar.HOUR_OF_DAY);
            int min = cal.get(Calendar.MINUTE);
            vFecha.setText((mes + 1) + "/" + dia + "  " + hora + ":" + min);
            listFechas.setVisible(false);
        }
        vTipo.setText(de.getTipo().toString());
    }
    
    private ImageIcon cargarImagen(String s) {
        try {
            ImageIcon imagen = new ImageIcon(new URL(s));
           return imagen;
        } catch (java.net.MalformedURLException e) {
            System.out.println("Error al cargar la URL");
        }
        return null;
    }
    
     private void llenarMuro(){
    
        Proxies.DataGrupo dg = ctrlDetalles.detallarGrupo(jComboBox_Grupos.getSelectedItem().toString());
      List<Proxies.DataContenido> cont = dg.getMuro().getContenidos();
      DefaultListModel model = new DefaultListModel();
      jListMuro.setModel(model);
      Proxies.DataContenido dc;
      Collections.reverse(cont);
      Proxies.DataUsuario u;
      ListIterator<Proxies.DataContenido> itera = cont.listIterator();
      while (itera.hasNext()){
      dc = itera.next();
      
      if(dc.getUrl() == null){
     
      u = dc.getAutor();
      String es = "____________________________________________________";
      model.addElement(u.getNombre().toString()); 
      model.addElement(dc.getTitulo().toString());
      model.addElement(dc.getCuerpo().toString());
      model.addElement(es);
      } else {
      ImageIcon img;
        u = dc.getAutor();
        img = cargarImagen(dc.getUrl());
        img.setDescription(u.getNombre().toString());
        String s = img.getDescription().toString();
        String tit = dc.getTitulo();
        String es = "____________________________________________________";
        model.addElement(s);
        model.addElement(tit);
        model.addElement(img);   
        model.addElement(es);
      }
        }
     }

}
